#include <iostream>
#include <math.h>

using namespace std;

void getInputs( double& a, double& b, double& c);
void runCalculations( double a, double b, double c, double& payments, double& totInterest, double& totalCashPaid);
void outputResults( double payments, double totInterest, double totalCashPaid);

int totalInterestPaid()
{
//declare variables
double totalLoan = 0;
double rate = 0;
double years = 0;
double monthlyPayments = 0;
double totalInterest = 0;
double totalCashPaidOut = 0;
char tryAgain;
bool keepGoing = true;

// welcome user
cout << "********************** Welcome **************************" << endl;
cout << "Ever wonder what's the total amount of interest you that" << endl;
cout << "you pay for a mortgage loan? The answer will shock you!" << endl;
cout << "Use this calculator to figure out how to pay the least" << endl;
cout << "amount of total interest!" << endl << endl ;


while(keepGoing)                        // keeps looking until user breaks out.
{
					
	getInputs(totalLoan, rate, years);  // run get inputs function
	runCalculations(totalLoan, rate, years, monthlyPayments, totalInterest, totalCashPaidOut);  //run very very complex math formulas
	outputResults(monthlyPayments, totalInterest, totalCashPaidOut);   // output results to user

	cout << "Would you like to recalculate again? (y/n) : ";   // if user doesnt want to continue, then we will break out of the loop
	cin >> tryAgain;

		if (tryAgain != 'y')
		{
			cout <<"Good Bye."<<endl;  
				keepGoing = false;   
		}
		else 
		{ cout << "" << endl << endl;
		}
}


system("pause");
    return 0;
}

//Functions
void getInputs (double& a, double& b, double& c)         //gather data from user
{



cout << "1. Enter the Total Loan Amount:           ";
cin >> a;

cout << "2. Enter Rate (whole numbers):            ";
cin >> b;

cout << "3. Enter total # of months to repay loan: ";
cin >> c;
cout << "" << endl;
cout << "Running Calculations... " << endl << endl;
}

void runCalculations (double a, double b, double c, double& payments, double& totInterest, double& totalCashPaid)   // perform calculations from user inputs
{
	double decimalRate = b/100;

payments = a / (( 1 - 1 / pow(1 + decimalRate, c)) / decimalRate);
totInterest = (payments * c) - a;
totalCashPaid = a + totInterest;
}

void outputResults (double payments, double totInterest, double totalCashPaid)   // return results to user
{
cout << "     Your monthly payment will be: $ "<< payments << endl;
cout << "     The total interest you will pay is: $ "<< totInterest <<endl;
cout << "     The total amount you will pay is: $ " << totalCashPaid << endl << endl << endl;
}
